 var path = require('path');

 var fs = require('fs');



 var mslibrary = require('ms-common-library');

 var download = require('./downloadFile');

 var zipInstall = require('./zip-install');

 module.exports = function (UC, source, config, ArFor, cache, package) {

     var source = config.Source;
     var UserControl = UC;
     var openjson = config.openjson;

     var installok = function (mid, body, Callback) {
         new source(mid, body, function (obj) {
             Callback()
         })
     };

     var installAppServer = function (PluginPath, package, body) {

         var setMessage = function (val) {
             var socketid = UserControl.Userid2Socketid(body.user_id);
             if (socketid) {
                 var Socket = config.Socket;
                 Socket.SetClientMessage(socketid, val)
             };
         }
         package.webos_node_model_type = 2;
         config.InstallApp(PluginPath, package, function () {
             config.installPackages[body.key] = package;
             var value = {
                 id: body.id,
                 error: 0,
                 "key": "install-plugin",
                 notify: {
                     duration: 0,
                     title: '@plugin_successful_installation',
                     message: package.label,
                     type: 'success'
                 }
             };

             if (package.AppsInitFunctions) {
                 value.AppsInitFunctions = package.AppsInitFunctions;
             }

             installok("M_678", body, function () {
                 setMessage(value)
             });
         })
     }
     var installPackage = function (body, Callback) {
         new source("M_671", body, function (obj) {
             if (obj && obj.localpath) {
                 var PluginPath = path.join(config.Files.Plugin, obj.localpath);
                 var packagefile = path.join(PluginPath, 'package.json');
                 openjson(packagefile, function (error, db) {
                     var package = db;
                     if (error) {
                         var value = {
                             error: 109
                         };
                         Callback(value)
                     } else {
                         body.key = obj.key;
                         new installAppServer(PluginPath, package, body);
                         var value = {
                             error: 0
                         };
                         Callback(value)
                     }
                 });
             } else {
                 var ajaxTest = {
                     error: 0
                 };

                 Callback(ajaxTest)
             }
         })
     }
     var router = function (req, res, next) {
         var body = UserControl.GetForm(req.session.key, req.body);
         
         if (body.appid) {

             if (config.PluginPool.get(body.appid)) {
                 var elm = config.PluginPool.get(body.appid);

                 var localpath = mslibrary.RandomString();

                 var PlugindownloadPath = path.join(config.Files.Plugin, localpath + '.zip')
                 var off = false
                 download(elm.url, PlugindownloadPath, function (error, _off) {

                     if (!off) {
                         off = true;
                         body.localpath = localpath;
                         body.zipfile = PlugindownloadPath;
                         zipInstall(source, body, config, function (obj, id) {

                             if (obj.error == 0 && id) {
                                 var body = UserControl.GetForm(req.session.key, req.body);

                                 body.id = id;
                                 installPackage(body, function () {

                                 })

                             }
                         })
                     }

                 })
                 var ajaxTest = {
                     error: 0
                 };

                 res.jsonp(ajaxTest);
             } else {

                 var ajaxTest = {
                     error: 110
                 };
                 res.jsonp(ajaxTest);
             }
         } else if (body.id) {
             installPackage(body, function (value) {
                 res.Json(value)
             })
         } else {

             var ajaxTest = {
                 error: 111
             };
             res.jsonp(ajaxTest);
         }
     };

     return router;

 }